﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Common
{
	[Serializable]
	public class CrawlJobInfo
	{
		public int JobId { set; get; }
		public int UserId { set; get; }
		public string Name { set; get; }
		public string Urls { set; get; }
		public string Encoding { set; get; }
		public int Speed { set; get; }
		public DateTime StartTime { set; get; }
		public DateTime EndTime { set; get; }
		public int Status { set; get; }
		public string StatusString { 
			get{
				if(this.Status == 1){
					return "启动";
				}
				return "停止";
			}
		}
		public CrawlJobFileInfo[] Files{ set; get; }

		public static CrawlJobInfo FillData(IDataReader dr)
		{
			CrawlJobInfo item = new CrawlJobInfo();
			item.JobId = Convert.ToInt32(dr["job_id"]);
			item.UserId = Convert.ToInt32(dr["uid"]);
			item.Name = Convert.ToString(dr["name"]);
			item.Speed = Convert.ToInt32(dr["speed"]);
			item.Encoding = Convert.ToString(dr["encoding"]);
			item.Urls = Convert.ToString(dr["seed_urls"]);
			item.Status = Convert.ToInt32(dr["Status"]);
			
			if (dr["start_time"] != DBNull.Value)
			{
				item.StartTime = Convert.ToDateTime(dr["start_time"]);
			}

			if (dr["end_time"] != DBNull.Value)
			{
				item.EndTime = Convert.ToDateTime(dr["end_time"]);
			}
			return item;
		}

		public static CrawlJobInfo[] FillDataCollection(IDataReader dr)
		{
			List<CrawlJobInfo> list = new List<CrawlJobInfo>();
			while (dr.Read())
			{
				CrawlJobInfo item = FillData(dr);
				list.Add(item);
			}
			if (list.Count == 0)
			{
				return null;
			}
			return list.ToArray();
		}
	}

	[Serializable]
	public class CrawlJobFileInfo
	{
		public int JobId { set; get; }
		public int FileId { set; get; }
		public int UserId { set; get; }
		public string Name { set; get; }
		public string UrlPattern { set; get; }
		public string FeedRecUrl { set; get; }
		public string Lua { set; get; }
		public bool IsLoadLink { set; get; }

		public static CrawlJobFileInfo FillData(IDataReader dr)
		{
			CrawlJobFileInfo item = new CrawlJobFileInfo();
			item.FileId = Convert.ToInt32(dr["file_id"]);
			item.JobId = Convert.ToInt32(dr["job_id"]);
			item.UserId = Convert.ToInt32(dr["uid"]);
			item.Name = Convert.ToString(dr["name"]);
			item.UrlPattern = Convert.ToString(dr["url_pattern"]);
			item.FeedRecUrl = Convert.ToString(dr["fee_rec_url"]);
			item.Lua = Convert.ToString(dr["lua"]);
			item.IsLoadLink = Convert.ToBoolean(Convert.ToByte(dr["is_load_link"]));
			return item;
		}

		public static CrawlJobFileInfo[] FillDataCollection(IDataReader dr)
		{
			List<CrawlJobFileInfo> list = new List<CrawlJobFileInfo>();
			while (dr.Read())
			{
				CrawlJobFileInfo item = FillData(dr);
				list.Add(item);
			}
			if (list.Count == 0)
			{
				return null;
			}
			return list.ToArray();
		}
	}

	[Serializable]
	public class CrawlJobFileFieldInfo
	{
		public int FieldId { set; get; }
		public int FileId { set; get; }
		public int DataType { set; get; }
		public int DataLen { set; get; }
		public int DataPreci { set; get; }
		public int UserId { set; get; }
		public string Name { set; get; }
		public string Code { set; get; }
		public string XPath { set; get; }
		public string Lua { set; get; }

		public static CrawlJobFileFieldInfo FillData(IDataReader dr)
		{
			CrawlJobFileFieldInfo item = new CrawlJobFileFieldInfo();
			item.FieldId = Convert.ToInt32(dr["field_id"]);
			item.DataType = Convert.ToInt32(dr["data_type"]);
			item.DataLen = Convert.ToInt32(dr["data_len"]);
			item.DataPreci = Convert.ToInt32(dr["data_preci"]);
			item.FileId = Convert.ToInt32(dr["file_id"]);
			item.UserId = Convert.ToInt32(dr["uid"]);
			item.Name = Convert.ToString(dr["name"]);
			item.Code = Convert.ToString(dr["code"]);
			item.XPath = Convert.ToString(dr["xpath"]);
			item.Lua = Convert.ToString(dr["lua"]);
			return item;
		}

		public static CrawlJobFileFieldInfo[] FillDataCollection(IDataReader dr)
		{
			List<CrawlJobFileFieldInfo> list = new List<CrawlJobFileFieldInfo>();
			while (dr.Read())
			{
				CrawlJobFileFieldInfo item = FillData(dr);
				list.Add(item);
			}
			if (list.Count == 0)
			{
				return null;
			}
			return list.ToArray();
		}
	}
}
